package com.ssm.controller;

import com.ssm.mapper.InformationMapper;
import com.ssm.pojo.Demand;
import com.ssm.pojo.Information;
import com.ssm.pojo.UserInformation;
import com.ssm.service.OperationMessage;
import com.ssm.service.UserLogin;
import com.ssm.utils.UserContext;
import org.codehaus.xfire.transport.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Login {

    @Resource(name = "userLoginImp")
    private UserLogin userLoginImp;

    @RequestMapping(value="/login")
    @ResponseBody
    public Map login(String user, String pwd, HttpServletRequest request){
        //  state 0,成功，1、失败，2、传入参数为空，3、账号或者密码错误
        HashMap<Object, Object> userMap = new HashMap<>();
        if(user != null && pwd != null){
            UserInformation userInformation = new UserInformation();
            userInformation.setPassWord(pwd);
            userInformation.setUserId(user);
            try {
                List<UserInformation> userList = userLoginImp.selectUser(userInformation);
                if(userList.size() == 0){
                    userMap.put("state",3);
                }else{
                    userMap.put("state",0);
                    request.getSession().setAttribute("USER",userList.get(0));
                    userMap.put("data",userList);
                }
            }catch (Exception e){
                userMap.put("state",1);
            }
        }else{
            userMap.put("state",2);
        }
        return userMap;
    }

    @RequestMapping(value = "/")
    public String index(){
        return "index";
    }
}
